import {
  ClearOriginCommentAction,
  ClearReplyCommentsAction,
  getReplyCommentsByIdAction,
} from '@/store/action/comments';
import { useAppSelector } from '@/store/reducer';
import { NavBar } from 'antd-mobile';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import CommentFooter from '../CommentFooter';
import CommentItem from '../CommentItem';
import NoComment from '../NoComment';
import styles from './index.module.scss';
export default function CommentReply() {
  const dispatch = useDispatch();
  const {
    originComment,
    replyComments: { results },
  } = useAppSelector((state) => state.comments);
  const { reply_count } = originComment;

  useEffect(() => {
    if (!originComment.com_id) return;
    dispatch(
      getReplyCommentsByIdAction({ type: 'c', source: originComment.com_id })
    );
    return () => {};
  }, [dispatch, originComment.com_id]);

  // 返回移除数据
  const handleClear = () => {
    dispatch(ClearOriginCommentAction());
    dispatch(ClearReplyCommentsAction());
  };

  return (
    <div className={styles.root}>
      <div className="reply-wrapper">
        {/* 顶部导航栏 */}
        <NavBar className="transparent-navbar" onBack={handleClear}>
          <div>{originComment.reply_count}条回复</div>
        </NavBar>

        {/* 原评论信息 */}
        <div className="origin-comment">
          <CommentItem comment={originComment}></CommentItem>
        </div>

        {/* 回复评论的列表 */}
        <div className="reply-list">
          <div className="reply-header">全部回复</div>
          {reply_count > 0 ? (
            results.map((item) => (
              <CommentItem comment={item} key={item.com_id}></CommentItem>
            ))
          ) : (
            <NoComment />
          )}
        </div>

        {/* 评论工具栏，设置 type="reply" 不显示评论和点赞按钮 */}
        <CommentFooter type="reply" />
      </div>
    </div>
  );
}
